
#include "Bullet.h"
#include "Enemy.h"
#include <math.h>
#include "../../SGD Wrappers/CSGD_TextureManager.h"

//Constructor
Bullet::Bullet(void)
{
	this->x					= 0.0f;
	this->y					= 0.0f;
	this->velocityX			= 0.0f;
	this->velocityY			= 0.0f;
	this->rotation			= 0.0f;
	this->collisionRadius	= 1.0f;
	this->isAlive			= true;
	this->entityType		= ENTITY_BULLET;
	this->bulletType		= BULLET_BASIC;
	this->moveSpeed			= 400.0f;
	this->attackTarget		= nullptr;
	this->damage			= 1;
	this->textureID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("../Resource/Graphics/PlaceholderImage.png"));
}


//Destructor
Bullet::~Bullet(void)
{
	if (attackTarget != nullptr)
		attackTarget->RemoveReference();
}


//Update
void Bullet::Update(float dt)
{
	//Clear target if it has died
	if (attackTarget != nullptr && attackTarget->isAlive == false)
	{
		attackTarget->RemoveReference();
		attackTarget = nullptr;
	}

	//Move toward the target
	if (attackTarget != nullptr)
	{
		float vectorToTargetX   = attackTarget->x - x;
		float vectorToTargetY   = attackTarget->y - y;
		float magnitude = sqrt(vectorToTargetX*vectorToTargetX + vectorToTargetY*vectorToTargetY);
		if (magnitude > moveSpeed * dt)
		{
			vectorToTargetX /= magnitude;
			vectorToTargetY	/= magnitude;
			x += vectorToTargetX * moveSpeed * dt;
			y += vectorToTargetY * moveSpeed * dt;
		}
		else
		{
			vectorToTargetX /= magnitude;
			vectorToTargetY	/= magnitude;
			x += vectorToTargetX * magnitude;
			y += vectorToTargetY * magnitude;
		}
	}

	//Set rotation
	if (attackTarget != nullptr)
	{
		float newRotation = rotation;
		float vectorToTargetX = attackTarget->x - x;
		float vectorToTargetY = attackTarget->y - y;
		newRotation = atan2(vectorToTargetY, vectorToTargetX) + 3.141f * 0.5f;
		if (newRotation >  6.283f) newRotation -= 6.281f;
		if (newRotation < -0.001f) newRotation += 6.281f;
		float rotationRate = 8.0f * dt;
		if (rotation - newRotation < -0.001f)
		{
			if (fabs(rotation - newRotation) > 3.141f) rotation -= rotationRate;
			else rotation += rotationRate;
		}
		if (rotation - newRotation >  0.001f)
		{
			if (fabs(rotation - newRotation) > 3.141f) rotation += rotationRate;
			else rotation -= rotationRate;
		}
		if (rotation >  6.283f) rotation -= 6.281f;
		if (rotation < -0.001f) rotation += 6.281f;
	}

	//Hit the target if it is close enough
	if (attackTarget != nullptr)
	{
		if ((sqrt((attackTarget->x - x)*(attackTarget->x - x) + (attackTarget->y - y)*(attackTarget->y - y))) <= 28.0f)
		{
			//Hit target
			dynamic_cast<Enemy*>(attackTarget)->TakeDamage(damage);
			attackTarget->RemoveReference();
			attackTarget = nullptr;
		}
	}

	//Die if there is no attack target
	if (attackTarget == nullptr)
	{
		isAlive = false;
	}
}


//Render
void Bullet::Render()
{
	CSGD_TextureManager* TM = CSGD_TextureManager::GetInstance();
	RECT rect = {0, 0, 64, 64};
	POINT ptCenter = { (rect.right - rect.left) / 2, (rect.bottom - rect.top) / 2 };
	TM->Draw(textureID, (int)x - 9, (int)y - 9, 0.3f, 0.3f, &rect,
		(float)ptCenter.x, (float)ptCenter.y, rotation, D3DCOLOR_ARGB(255,255,255,255));

	TM->Draw(textureID, (int)x-3, (int)y-3, 0.1f, 0.1f, &rect, 32.0f, 32.0f, rotation, D3DCOLOR_ARGB(255,255,50,50));
}
